package cn.edu.scau.cmi.xuxingao.controller.adapter;
/**
 * Sample Skeleton for 'AdapterController.fxml' Controller Class
 */

import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.ResourceBundle;

import cn.edu.scau.cmi.xuxingao.adapter.AdapterOfClass;
import cn.edu.scau.cmi.xuxingao.adapter.AdapterOfObject;
import cn.edu.scau.cmi.xuxingao.adapter.Student;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.PropertyValueFactory;

public class AdapterController {

    @FXML // ResourceBundle that was given to the FXMLLoader
    private ResourceBundle resources;

    @FXML // fx:id="tiptext"
    private Label tiptext; // Value injected by FXMLLoader
    
    @FXML // URL location of the FXML file that was given to the FXMLLoader
    private URL location;

    @FXML // fx:id="idTextField"
    private TextField idTextField; // Value injected by FXMLLoader

    @FXML // fx:id="nameTextField"
    private TextField nameTextField; // Value injected by FXMLLoader

    @FXML // fx:id="enrollBtn"
    private Button enrollBtn; // Value injected by FXMLLoader

    @FXML // fx:id="classTable"
    private TableView classTable; // Value injected by FXMLLoader

    @FXML // fx:id="classIdColumn"
    private TableColumn classIdColumn; // Value injected by FXMLLoader

    @FXML // fx:id="classNameColumn"
    private TableColumn classNameColumn; // Value injected by FXMLLoader

    @FXML // fx:id="objectTable"
    private TableView objectTable; // Value injected by FXMLLoader

    @FXML // fx:id="objectIdColumn"
    private TableColumn objectIdColumn; // Value injected by FXMLLoader

    @FXML // fx:id="objectNameColumn"
    private TableColumn objectNameColumn; // Value injected by FXMLLoader

    private final ObservableList<Student> stuOList = FXCollections.observableArrayList();
    private final ObservableList<Student> stuOList1 = FXCollections.observableArrayList();
    private int id;
    private String name;
    private boolean flag=false;
    private List <Integer> intList = new ArrayList<Integer>();
    private List <Integer> intList1 = new ArrayList<Integer>();
    private List <Student> stuList = new ArrayList<Student>();
    private List <Student> stuList1 = new ArrayList<Student>();
    @FXML
    
    void enroll(ActionEvent event) {
    	if(getInfo()!=null) {
    		id = Integer.parseInt(idTextField.getText());
    		name = nameTextField.getText();
    		while(name.equals("")) {
    			tiptext.setText("请输入符合规范的学号和姓名！");}
    		idTextField.clear();
    		nameTextField.clear();
        	intList.add(id);
        	stuList.add(new Student(id,name));
        	intList1.add(id);
        	stuList1.add(new Student(id,name));
        	
        	AdapterOfClass adapterofclass = new AdapterOfClass();
        	adapterofclass.sortInt(intList);
        	adapterofclass.sortStudent(intList, stuList);
        	
        	AdapterOfObject adapterofobject = new AdapterOfObject();
        	adapterofobject.sortInt(intList1);
        	adapterofobject.sortStudent(intList1, stuList1);
        	
        	classIdColumn.setCellValueFactory(new PropertyValueFactory<>("id"));
        	classNameColumn.setCellValueFactory(new PropertyValueFactory<>("name"));
        	objectIdColumn.setCellValueFactory(new PropertyValueFactory<>("id"));
        	objectNameColumn.setCellValueFactory(new PropertyValueFactory<>("name"));
        	stuOList.clear();
        	stuOList.addAll(stuList);
        	stuOList1.clear();
        	stuOList1.addAll(stuList1);
        	if(!flag) {
        		classTable.setItems(stuOList);
        		objectTable.setItems(stuOList1);
        		flag=true;
        	}
        	tiptext.setText("");
    	}
    }

    Student getInfo() {
    	try{id = Integer.parseInt(idTextField.getText());}
    	catch(Exception e) {
    		tiptext.setText("请输入符合规范的学号和姓名！");
    		return null;
    	}
		name = nameTextField.getText();
		if(name.equals("")) {
			tiptext.setText("请输入符合规范的学号和姓名！");
			return null;
		}
		return new Student(id,name);
	}
    @FXML // This method is called by the FXMLLoader when initialization is complete
    void initialize() {
        assert idTextField != null : "fx:id=\"idTextField\" was not injected: check your FXML file 'AdapterController.fxml'.";
        assert nameTextField != null : "fx:id=\"nameTextField\" was not injected: check your FXML file 'AdapterController.fxml'.";
        assert enrollBtn != null : "fx:id=\"enrollBtn\" was not injected: check your FXML file 'AdapterController.fxml'.";
        assert classTable != null : "fx:id=\"classTable\" was not injected: check your FXML file 'AdapterController.fxml'.";
        assert classIdColumn != null : "fx:id=\"classIdColumn\" was not injected: check your FXML file 'AdapterController.fxml'.";
        assert classNameColumn != null : "fx:id=\"classNameColumn\" was not injected: check your FXML file 'AdapterController.fxml'.";
        assert objectTable != null : "fx:id=\"objectTable\" was not injected: check your FXML file 'AdapterController.fxml'.";
        assert objectIdColumn != null : "fx:id=\"objectIdColumn\" was not injected: check your FXML file 'AdapterController.fxml'.";
        assert objectNameColumn != null : "fx:id=\"objectNameColumn\" was not injected: check your FXML file 'AdapterController.fxml'.";

    }
}
